<?php

namespace App\Http\Controllers\SubAccount;

use App\Common\Traits\Model\DepartmentSearchTrait;
use App\Common\Traits\Model\ModelSearchTrait;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Models\SubAccount\SubAccountDataRecord;
use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use App\Models\SubAccount\SubAccountStaffBind;
use Illuminate\Http\Request;

class SubAccountDataRecordController extends Controller
{
    use ModelSearchTrait, DepartmentSearchTrait, ServiceTrait;

    //
    public function index(Request $request, SubAccountDataRecord $dataRecord){
        $dataRecord=$this->model($dataRecord,$request->user());
        $dataRecord=$this->singleModelSearch($request,$dataRecord,$this->searchCondition());
        $dataRecord=$this->departmentSearch($request,$dataRecord);
        $records=$dataRecord->orderByDesc('id')->paginate($request->size??20)->withQueryString();
        return $this->success($records);
    }
    //
    public function searchCondition(){
        return [
            ['platform_name','like'],
            ['record_staff','like'],
            ['sub_account','like'],
            ['record_time','between'],
        ];
    }

    //
    public function dataRecord(Request $request){
        $loginLog=SubAccountLoginLogoutRecord::find($request->record_id??0);
        if(empty($loginLog)){
            return $this->failed('记录id错误',201);
        }
        if($loginLog->is_data_recorded){
            return $this->failed('此上号记录已登记过数据，无需再次登记',201);
        }
        if(!$this->checkDataRecordPermission($loginLog,$request->user())){
            return $this->failed('无权限登记此账号数据',201);
        }
        $data=$this->recordData($loginLog);
        $data=$this->columnsData($request,$data);
        SubAccountDataRecord::create($data);
        $this->updateLoginLog($loginLog);
        return $this->success(['msg'=>'登记成功']);
    }
    //验证是否有数据登记权限
    public function checkDataRecordPermission($loginLog,$user){
        //绑定
        $count=SubAccountStaffBind::where('sub_account_id',$loginLog->sub_account_id)->where('staff_id',$user->id??0)->count('id');
        if($count){
            return true;
        }
        //上号
        $logs=SubAccountLoginLogoutRecord::where('record_code',$loginLog->record_code)->where('staff_id',$user->id??0)->count('id');
        if($logs){
            return true;
        }
        return false;
    }
    //
    public function recordData($loginLog){
        return [
            'record_time'=>now(),
            'sub_account_id'=>$loginLog->sub_account_id,
            'sub_account'=>$loginLog->sub_account,
            'department1_id'=>$loginLog->department1_id,
            'department1'=>$loginLog->department1,
            'department2_id'=>$loginLog->department2_id,
            'department2'=>$loginLog->department2,
            'department3_id'=>$loginLog->department3_id,
            'department3'=>$loginLog->department3,
            'department_id'=>$loginLog->department_id,
            'record_staff_id'=>auth()->user()->id??'',
            'record_staff'=>auth()->user()->name??'',
            'shop_id'=>$loginLog->shop_id,
            'shop_name'=>$loginLog->shop_name,
            'template_id'=>$loginLog->account->template_id??'',
            'platform_id'=>$loginLog->shop->plateform_id??'',
            'platform_name'=>$loginLog->shop->platform->platform_name??'',
        ];
    }
    //
    public function columnsData($request,$data){
        for($i=1;$i<=20;$i++){
            $column='custom_column'.$i;
            if($request->filled($column)){
                $data[$column]=$request->$column;
            }
        }
        return $data;
    }
    //
    public function updateLoginLog($loginLog){
        return SubAccountLoginLogoutRecord::where('record_code',$loginLog->record_code)->update(['is_data_recorded'=>1]);
    }

}
